package com.hqw.jzdemo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.androidquery.AQuery;
import com.fengfei.ffadsdk.AdViews.Native.FFNativeInfo;
import com.fengfei.ffadsdk.AdViews.Native.FFNativeLoadListener;
import com.fengfei.ffadsdk.AdViews.Native.FFNativeManager;
import com.fengfei.ffadsdk.AdViews.Native.FFNativeShowListener;
import com.fengfei.ffadsdk.AdViews.Native.FFNativeViewContainer;
import com.fengfei.ffadsdk.Common.Util.FFAdLogger;

import java.util.ArrayList;
import java.util.List;

import static com.hqw.jzdemo.util.Util.showToast;

public class CustomNativeActivity extends AppCompatActivity {

    private Context mContext;
    private AQuery mAQuery;
    private ViewGroup viewGroup;

    private FFNativeViewContainer mContainer;
    private FFNativeManager nativeManager;
    private FFNativeInfo mAdInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_native);
        mContext = this;
        initView();
    }

    private void initView() {
        mAQuery = new AQuery(findViewById(R.id.root));
        mContainer = findViewById(R.id.custom_native_container);

        Button loadBtn = findViewById(R.id.loadBtn);
        Button showBtn = findViewById(R.id.showBtn);

        loadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadAd();
                showToast(mContext, "开始请求");
            }
        });
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdInfo != null) {
                    initAd(mAdInfo);
                }
            }
        });
    }

    private void loadAd() {
        nativeManager = new FFNativeManager(this);
        nativeManager.requestAd(this, BuildConfig.appId, BuildConfig.nativeId, new FFNativeLoadListener() {
            @Override
            public void onNativeAdReceived(ArrayList<FFNativeInfo> infoArrayList) {
                Log.i("FFAd-native", "onNativeAdReceived()");
                mAdInfo = infoArrayList.get(0);
                showToast(mContext, "数据请求完毕: " + infoArrayList.size());
            }

            @Override
            public void onNativeAdReceiveFailed(String failReason) {
                Log.e("FFAd-native", "onNativeAdReceived(): " + failReason);
                mAdInfo = null;
                showToast(mContext, "数据请求失败 " + failReason);
            }

            @Override
            public void onDownloadStatusChange(int status) {

            }
        }, true);
    }

    private void initAd(FFNativeInfo ad) {
        // 出现在屏幕内后调用
        renderAd(ad);

        int adType = ad.getAdType(); // 广告类型
        Log.i("FFAd-native", "adType: " + adType);
        List<View> clickableViews = new ArrayList<>();
        // Note: 这里只处理单图的情况
        if (adType == 1) {
            clickableViews.add(viewGroup);

            clickableViews.add(findViewById(R.id.custom_native_img_view));
            clickableViews.add(findViewById(R.id.custom_native_title_view));
            clickableViews.add(findViewById(R.id.custom_native_desc_view));
        }

        nativeManager.showAd(mContainer, clickableViews, new FFNativeShowListener() {
            @Override
            public void onNativeAdShowFail(String failReason) {
                FFAdLogger.e("FFNativeManager.onNativeAdShowFail(): " + failReason);
            }

            @Override
            public void onNativeAdClicked() {
                FFAdLogger.i("FFNativeManager.onNativeAdClicked()");
            }

            @Override
            public void onNativeAdExposured() {
                FFAdLogger.i("FFNativeManager.onNativeAdExposured()");
            }
        });

    }

    private void renderAd(FFNativeInfo ad) {
        int adType = ad.getAdType(); // 0 模版; 1 单图; 2 三小图; 3 视频
        viewGroup = findViewById(R.id.custom_native_view);
        viewGroup.removeAllViews();
        // Note: 这里只处理单图的情况
        if (adType == 1) {
            mAQuery.id(R.id.custom_native_img_view).image(ad.getkAdImageUrl(), false, true);
            mAQuery.id(R.id.custom_native_title_view).text(ad.getkAdTitle());
            mAQuery.id(R.id.custom_native_desc_view).text(ad.getkAdDesc());
        }

        mAQuery.id(R.id.custom_native_adTag_view).text(ad.getkAdTagName());
    }

    @Override
    protected void onResume() {
        FFAdLogger.i("FFNativeManager.onResume()");
        super.onResume();
        if (nativeManager != null) nativeManager.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FFAdLogger.i("FFNativeManager.onDestroy()");
        if (nativeManager != null) nativeManager.destroy();
    }

}
